package com.newbee.adapter;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hhu.newbee.R;
import com.newbee.listbean.VoiceFileBean;
import com.newbee.sound.Sound;

public class VoiceFileListvewAdapter extends BaseAdapter
{

	private List<VoiceFileBean> list;
	private Context context;

	public VoiceFileListvewAdapter(List<VoiceFileBean> list, Context context)
	{
		this.list = list;
		this.context = context;
	}

	@Override
	public int getCount()
	{
		if (list != null)
		{
			return list.size();
		}
		else
		{
			return 0;
		}

	}

	@Override
	public Object getItem(int item)
	{
		return item;
	}

	@Override
	public long getItemId(int item)
	{
		return item;
	}

	@Override
	public View getView(int position, View view, ViewGroup arg2)
	{
		ViewHolder hold = new ViewHolder();

		if (view == null)
		{
			view = LayoutInflater.from(context).inflate(
					R.layout.listitem_voice_recong, null);
		}
		hold.dur = (TextView) view.findViewById(R.id.voice_durtion);
		hold.date = (TextView) view.findViewById(R.id.voice_recong_time);
		hold.isTrans = (TextView) view.findViewById(R.id.voice_file_tag);

		VoiceFileBean vrlb = list.get(position);

		Integer hour = Integer.parseInt(vrlb.getVftime().split(" ")[1]
				.substring(0, 2));

		StringBuffer sb = new StringBuffer(vrlb.getVftime().subSequence(0, 10));
		sb.append(" ");
		if (hour > 12)
		{
			sb.append("下午");
			hour = hour - 12;
		}
		else
		{
			sb.append("上午");
		}
		sb.append(hour);
		sb.append(vrlb.getVftime().subSequence(13, 16));

		hold.date.setText(sb.toString());

		hold.dur.setText(vrlb.getVlength());

		File file = new File(Sound.DIR + vrlb.getVfname().split("\\.")[0]
				+ ".vtf");

		if (file.exists())
		{

			hold.isTrans.setText("已经完成音文转换");
			hold.isTrans.setTextColor(context.getResources().getColor(
					R.color.steelblue));

		}
		else
		{
			hold.isTrans.setText("未完成音文转换");
			hold.isTrans.setTextColor(context.getResources().getColor(
					R.color.gray));
		}
		//view.setTag(hold);
		return view;
	}

	static class ViewHolder
	{
		TextView date;
		TextView dur;
		TextView isTrans;

	}

	public List<VoiceFileBean> getList()
	{
		return list;
	}

	public void setList(List<VoiceFileBean> list)
	{
		this.list = list;
	}

}
